const path = require('path')
const ExtractTextPlugin = require('extract-text-webpack-plugin')

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: ['mocha'],
        files: [
            'test/**/*.js'
        ],

        preprocessors: {
            // add webpack as preprocessor
            'scripts/**/*.js': ['webpack', 'sourcemap'],
            'test/**/*.js': ['webpack', 'sourcemap']
        },

        webpack: { //kind of a copy of your webpack config
            devtool: 'inline-source-map', //just do inline source maps instead of the default
            module: {
                loaders: [
                    {
                        test: /\.js$/,
                        loader: 'babel',
                        exclude: path.resolve(__dirname, 'node_modules'),
                        query: {
                            presets: ['airbnb']
                        }
                    },
                    {
                        test: /\.json$/,
                        loader: 'json',
                    },
                    , {
                        test: /\.scss$/,
                        // ExtractTextPlugin.extract([notExtractLoader], loader, [options]) https://github.com/webpack/extract-text-webpack-plugin#api
                        loader: ExtractTextPlugin.extract('style-loader', 'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]__[hash:base64:5]!postcss-loader!sass-loader')
                    }
                    , {
                        test: /\.(jpe?g|png|gif|svg)$/i,
                        loaders: [
                            'file?hash=sha512&digest=hex&name=./tmp/[hash].[ext]',
                            // https://github.com/tcoopman/image-webpack-loader
                            'image-webpack'
                        ]
                    }
                ]
            },
            externals: {
                'react/lib/ExecutionEnvironment': true,
                'react/lib/ReactContext': true
            },
            sassLoader: {
                encoding: "utf-8"
            },
            plugins: [
                new ExtractTextPlugin('style.css', { allChunks: true }),
                // new webpack.DefinePlugin({
                //     'process.env': {
                //         'NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
                //     }
                // })
            ],
        },

        webpackServer: {
            noInfo: true //please don't spam the console when running in karma!
        },

        plugins: [
            'karma-webpack',
            'karma-mocha',
            'karma-sourcemap-loader',
            'karma-chrome-launcher',
            'karma-phantomjs-launcher'
        ],

        babelPreprocessor: {
            options: {
                presets: ['airbnb']
            }
        },

        reporters: ['progress'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        singleRun: false
    })
};
